package com.xgame.server.handler.impl;

import com.google.protobuf.InvalidProtocolBufferException;
import com.xgame.server.dbtool.AccountCsTool;
import com.xgame.server.dbtool.AccountillegalCsTool;
import com.xgame.server.dbtool.GameUtilTool;
import com.xgame.server.handler.Handler;
import com.xgame.server.netty.enity.MsgEntity;
import com.xgame.server.netty.proto.Cmdcode;
import com.xgame.server.netty.proto.Game;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class AccountHandler implements Handler {

    private static final Logger logger = LoggerFactory.getLogger(AccountHandler.class);

    @Resource
    private GameUtilTool gameUtilTool;

    @Resource
    private AccountillegalCsTool accountillegalCsTool;

    @Resource
    private AccountCsTool accountCsTool;

    @Override
    public void doHandler(MsgEntity msgEntity, List<MsgEntity> commandList) {
        Game.AccountDataCS account = SerialBytes2Object(msgEntity.getData());
        if (account == null) {
            logger.error("Account Parse error!！");
            return;
        }

        if (msgEntity.getCmdCode() == (int) Cmdcode.SaveCmdCode.CS_ACCOUNT_SAVE_REQ.getNumber()) {
            //上传命令
            try {
                //首先判断是否有重复的玩家id，
                int check = accountCsTool.checkHasValidAccount(account);

                if (check > 0) {
                    accountillegalCsTool.insertAccountRecord(account);
                    MsgEntity resRt = gameUtilTool.getCommonResult(msgEntity.getChannel(), Cmdcode.SaveCmdCode.CS_ACCOUNT_SAVE_RES.getNumber(), 99, 0, 0, check);
                    msgEntity.getChannel().writeAndFlush(resRt);// 发送消息
                    return;
                }

                Integer result = accountCsTool.selectAccountByAccount(account.getAccount()).size();
                if (result > 0) {

                    //首先校验时间
                    if (accountCsTool.checkValidDate(account)) {
                        accountCsTool.updateAccount(account);
                    } else {
                        MsgEntity resRt = gameUtilTool.getCommonResult(msgEntity.getChannel(), Cmdcode.SaveCmdCode.CS_ACCOUNT_SAVE_RES.getNumber(), 99, 0, 0, 2);
                        msgEntity.getChannel().writeAndFlush(resRt);// 发送消息
                        return;
                    }

                } else {
                    accountCsTool.insertAccount(account);
                }

                MsgEntity resRt = gameUtilTool.getCommonResult(msgEntity.getChannel(), Cmdcode.SaveCmdCode.CS_ACCOUNT_SAVE_RES.getNumber(), 99, 0, 1, 0);
                msgEntity.getChannel().writeAndFlush(resRt);// 发送消息

            } catch (Exception e) {
                //处理失败,错误代码：1
                logger.error("account:" + account.getAccount() + " " + e.toString());
                MsgEntity resRt = gameUtilTool.getCommonResult(msgEntity.getChannel(), Cmdcode.SaveCmdCode.CS_ACCOUNT_SAVE_RES.getNumber(), 99, 0, 0, 1);
                msgEntity.getChannel().writeAndFlush(resRt);// 发送消息
            }
        }
    }

    public static Game.AccountDataCS SerialBytes2Object(byte[] bytes) {
        try {
            return Game.AccountDataCS.parseFrom(bytes);
        } catch (InvalidProtocolBufferException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static byte[] SerialBytes2Object(Game.AccountDataCS data) {
        try {
            return data.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
